<?php




/**
* Add an entity in [ identifiers ]
*/

$identifiers = new identifiers();

if($variable->post('add-action', 'string', ''))
{
	# Posted Data: Apply security
	$data = $variable->post('identifiers', 'array', array());
	
	# Immediately activate the record
	$data['is_active']   = 'Y';
	$data['is_approved'] = 'Y';
	$data['identifier_context'] = 'GALLERY';
	
	# When this record is added for the first time?
	$data['added_on'] = 'UNIX_TIMESTAMP(CURRENT_TIMESTAMP())';
	
	# Bind the data edited, to the current subdomain only, if there is a subdomain_id column
	$data['subdomain_id'] = $subdomain_id;
	
	# If there are some FILEs to upload, please do now.
	# And assign the name of the file returned, to the same field.
	# $uploader = new uploader(__BASE__.'/templates/images');
	# $data['file_field'] = $uploader->store('file_field', '');
	
	# Produce the cropnail for uploaded images
	# $original_image = __BASE__.'/templates/images/'.$data['file_field'];
	# $cropnail_image = __BASE__.'/templates/images/thumbs/'.$data['file_field'];
	# $cropnail = new cropnail(200, 150); $cropnail->resize($original_image, $cropnail_image, 0);

	if($identifier_id = $identifiers->add($data, false))
	{
		# Treat/Patch something on the currently added record.
		# Case: Send a welcome message (and ask to authenticate), if applies, eg. as in Membership.
		# $identifiers->welcome_first($identifier_id);

		$messenger = new messenger('success', 'The record has been added.');

		stopper::url(url::last_page('identifiers-list.php'));
		# stopper::url('identifiers-add-successful.php');
		# stopper::url('identifiers-list.php');
		
		# Once successfully added, as the admin user to edit the data.
		# Inspired by some financial applications that allow to edit the record as soon as it is added.
		# @todo Generate it with the help of identifier_id and protection code
		# $code = '';
		# stopper::url("identifiers-edit.php?id={$identifier_id}&code={$code}");
	}
	else
	{
		$messenger = new messenger('error', 'The record was NOT added.');

		$_SESSION['attempt_identifiers'] = $data;
		stopper::url('identifiers-add.php');
		
		#stopper::url('identifiers-add-error.php');
	}
}
else
{
	# Must allow a chance to load the ADD form.
	# stopper::url('identifiers-direct-access-error.php');

	# Purpose of this code block is to make sure that the variable
	# gets all indices with blank data, to feed to ADD form.
	
	# A dynamic details about this record
	$details = array();
	
	if(isset($_SESSION['attempt_identifiers']))
	{
		$details = $_SESSION['attempt_identifiers'];
		unset($_SESSION['attempt_identifiers']);
	}

	# Validate it against the parameters in itself, plus those what we need.
	$details = $identifiers->validate('add', $details);
	$smarty->assign('identifiers', $details);
}

$smarty->assign('protection_code', $identifiers->code());
?>